package com.stony.poie.demo;

import java.math.BigDecimal;
import java.util.ArrayList;

import com.stony.poie.basic.XlsFile;
import com.stony.poie.basic.XlsTData;
import com.stony.poie.basic.XlsTable;
import com.stony.poie.demo.group.GroupDemoCategory;
import com.stony.poie.demo.group.GroupDemoGoods;
import com.stony.poie.demo.group.GroupDemoTable;
import com.stony.poie.demo.multiply.MultiplyDemoTRow;
import com.stony.poie.demo.multiply.MultiplyDemoTable;
import com.stony.poie.demo.simple.SimpleDemoTRow;
import com.stony.poie.demo.simple.SimpleDemoTable;
import com.stony.poie.ext.XlsFont;
import com.stony.poie.ext.XlsStyle;
import com.stony.poie.util.XlsEngine;

public class Main {

	public static void main(String args[]) throws Exception {

		/* Write excel file. */

		XlsFile xlsFile = new XlsFile(3);

		XlsStyle style = new XlsStyle();

		XlsFont font = new XlsFont();

		font.setBold(true);

		style.setFont(font);

		xlsFile.setStyle(0, style);

		xlsFile.setTable(0, createSimpleDemo());

		xlsFile.setTable(1, createMultiplyDemo(xlsFile));

		xlsFile.setTable(2, createComboDemo());

		XlsEngine engine = new XlsEngine();

		engine.setGroupRows(true);

		engine.exportXlsFile(xlsFile, "D:\\demo.xls");

	}

	/*
	 * 
	 * =============== Simple demo. ===============
	 * 
	 */

	private static XlsTable createSimpleDemo() {

		SimpleDemoTable table = new SimpleDemoTable();

		SimpleDemoTRow row = new SimpleDemoTRow();

		row.setName("Name");

		row.setTele("Tele");

		row.setMail("Mail");

		row.setMemo("Memo");

		table.setTitle(row);

		row = new SimpleDemoTRow();

		row.setName("Bill");

		row.setTele("123-456-789");

		row.setMail("bill@microsoft.com");

		row.setMemo("Microsoft");

		table.setBill(row);

		row = new SimpleDemoTRow();

		row.setName("Ellison");

		row.setTele("234-567-890");

		row.setMail("ellison@oracle.com");

		row.setMemo("Oracle");

		table.setEllison(row);

		row = new SimpleDemoTRow();

		row.setName("McNealy");

		row.setTele("345-678-901");

		row.setMail("mcnealy@sun.com");

		row.setMemo("Sun Microsystems");

		table.setMcnealy(row);

		row = new SimpleDemoTRow();

		row.setName("Palmisano");

		row.setTele("456-789-012");

		row.setMail("palmisano@ibm.com");

		row.setMemo("IBM");

		table.setPalmisano(row);

		return table;

	}

	/*
	 * 
	 * =============== Multiply demo. ===============
	 * 
	 */

	private static XlsTable createMultiplyDemo(XlsFile file) {

		MultiplyDemoTable table = new MultiplyDemoTable();

		table.setRows(new MultiplyDemoTRow[9]);

		for (int i = 1; i <= 9; i++) {

			MultiplyDemoTRow row = new MultiplyDemoTRow();

			row.setData(new ArrayList());

			for (int j = i; j <= 9; j++) {

				XlsTData data = new XlsTData();

				data.setValue(i + "*" + j + "=" + i * j);

				row.getData().add(data);

			}

			table.getRows()[i - 1] = row;

		}

		return table;

	}

	/*
	 * 
	 * =============== Combo demo. ===============
	 * 
	 */

	private static XlsTable createComboDemo() {

		GroupDemoTable table = new GroupDemoTable();

		GroupDemoCategory category = new GroupDemoCategory();

		category.setTitle(new XlsTData("Brand", 4, 1));

		GroupDemoGoods goods = new GroupDemoGoods();

		goods.setName("Zippo 205");

		goods.setPrice(new BigDecimal(410));

		goods.setQuantity(new BigDecimal(1000));

		goods.setDiscount(new BigDecimal(95));

		category.getGoods().add(goods);

		goods = new GroupDemoGoods();

		goods.setName("Philip HQ40");

		goods.setPrice(new BigDecimal(570));

		goods.setQuantity(new BigDecimal(5000));

		goods.setDiscount(new BigDecimal(90));

		category.getGoods().add(goods);

		goods = new GroupDemoGoods();

		goods.setName("Bel Popp");

		goods.setPrice(new BigDecimal(475));

		goods.setQuantity(new BigDecimal(1000));

		goods.setDiscount(new BigDecimal(88));

		category.getGoods().add(goods);

		goods = new GroupDemoGoods();

		goods.setName("Laneige 3104");

		goods.setPrice(new BigDecimal(580));

		goods.setQuantity(new BigDecimal(3000));

		goods.setDiscount(new BigDecimal(90));

		category.getGoods().add(goods);

		table.getGoodsList().add(category);

		table.getGoodsList().add(category);

		table.getGoodsList().add(category);

		return table;

	}

}
